using System.Collections.Generic;
using System.Threading;
namespace Rogan.Data
{
	public class EntityHelper
	{
		private static readonly Dictionary<string, object> EntityList = new Dictionary<string, object>();
		private static readonly object LockObj = new object();
		public static void Reset()
		{
			EntityList.Clear();
		}
		public static void Reset<T>() where T : Entity
		{
			string key = typeof(T).ToString();
			if (EntityList.ContainsKey(key))
			{
				EntityList.Remove(key);
			}
		}
		public static string GetName<T>() where T : Entity
		{
			T table = GetTable<T>();

			return table.GetName();
		}
		public static T GetTable<T>() where T : Entity
		{
			string key = typeof(T).ToString();
			if (EntityList.ContainsKey(key))
			{
				return (T)EntityList[key];
			}
			object obj;
			Monitor.Enter(obj = LockObj);
			T result;
			try
			{
				if (EntityList.ContainsKey(key))
				{
					result = (T)EntityList[key];
				}
				else
				{
					T t = DataHelper.Create<T>();
					EntityList.Add(key, t);
					result = t;
				}
			}
			finally
			{
				Monitor.Exit(obj);
			}
			return result;
		}
		public static T CreateTable<T>() where T : Entity
		{
			return DataHelper.Create<T>();
		}
		public static Field GetIdentityField<T>() where T : Entity
		{
			T table = GetTable<T>();
			return table.GetIdentityField();
		}
		public static Field[] GetPrimaryKeyFields<T>() where T : Entity
		{
			T table = GetTable<T>();
			return table.GetPrimaryKeyFields();
		}
		public static Field[] GetFields<T>() where T : Entity
		{
			T table = GetTable<T>();
			return table.GetFields();
		}
		public static Field GetFirstField<T>() where T : Entity
		{
			Field[] fields = GetFields<T>();
			if (fields != null && fields.Length > 0)
			{
				return fields[0];
			}
			return null;
		}
		public static bool IsReadOnly<T>() where T : Entity
		{
			T table = GetTable<T>();
			return table.IsReadOnly();
		}
		public static bool IsCompletedFild(string fieldName)
		{
			return fieldName.Trim() == "*" || fieldName.Contains("'") || fieldName.Contains("(") || fieldName.Contains(")") || fieldName.Contains("{0}") || fieldName.Contains("{1}") || fieldName.ToLower().Contains(" as ") || fieldName.Contains("*");
		}
	}
}
